<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>git 分支操作命令 | Finen&#39;s Blog</title>
    <meta name="generator" content="VuePress 1.4.1">
    <link rel="icon" href="/logo.png">
    <link rel="manifest" href="/manifest.json">
    <link rel="apple-touch-icon" href="/icons/apple-touch-icon-152x152.png">
    <link rel="mask-icon" href="/icons/safari-pinned-tab.svg" color="#3eaf7c">
    <meta name="description" content="Stay Hungry! Stay Foolish!">
    <meta name="theme-color" content="#3eaf7c">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="msapplication-TileImage" content="/icons/msapplication-icon-144x144.png">
    <meta name="msapplication-TileColor" content="#000000">
    <link rel="preload" href="/assets/css/0.styles.331bc503.css" as="style"><link rel="preload" href="/assets/js/app.8657dc36.js" as="script"><link rel="preload" href="/assets/js/3.655fda8f.js" as="script"><link rel="preload" href="/assets/js/60.e1ccc619.js" as="script"><link rel="preload" href="/assets/js/11.32858d9c.js" as="script"><link rel="prefetch" href="/assets/js/1.af5e15a9.js"><link rel="prefetch" href="/assets/js/10.3d57a2e0.js"><link rel="prefetch" href="/assets/js/12.cef9d203.js"><link rel="prefetch" href="/assets/js/13.02beb1f0.js"><link rel="prefetch" href="/assets/js/14.7efc03c9.js"><link rel="prefetch" href="/assets/js/15.6bbbab3f.js"><link rel="prefetch" href="/assets/js/16.f90edb71.js"><link rel="prefetch" href="/assets/js/17.0d4d6c13.js"><link rel="prefetch" href="/assets/js/18.76dd8979.js"><link rel="prefetch" href="/assets/js/19.7da82e6f.js"><link rel="prefetch" href="/assets/js/20.55ee1642.js"><link rel="prefetch" href="/assets/js/21.088a02d1.js"><link rel="prefetch" href="/assets/js/22.73d5a697.js"><link rel="prefetch" href="/assets/js/23.829e24c3.js"><link rel="prefetch" href="/assets/js/24.2f258433.js"><link rel="prefetch" href="/assets/js/25.0a52df22.js"><link rel="prefetch" href="/assets/js/26.1c66cc2f.js"><link rel="prefetch" href="/assets/js/27.aa781144.js"><link rel="prefetch" href="/assets/js/28.b2cf7947.js"><link rel="prefetch" href="/assets/js/29.b61b1598.js"><link rel="prefetch" href="/assets/js/30.ba0ae8b1.js"><link rel="prefetch" href="/assets/js/31.7637d043.js"><link rel="prefetch" href="/assets/js/32.e552a016.js"><link rel="prefetch" href="/assets/js/33.334376b0.js"><link rel="prefetch" href="/assets/js/34.08c95f99.js"><link rel="prefetch" href="/assets/js/35.76b6b95d.js"><link rel="prefetch" href="/assets/js/36.539b6d80.js"><link rel="prefetch" href="/assets/js/37.850783a5.js"><link rel="prefetch" href="/assets/js/38.06c33708.js"><link rel="prefetch" href="/assets/js/39.85fc61d5.js"><link rel="prefetch" href="/assets/js/4.12c21fd1.js"><link rel="prefetch" href="/assets/js/40.cdc93796.js"><link rel="prefetch" href="/assets/js/41.be79357e.js"><link rel="prefetch" href="/assets/js/42.e1ea76e5.js"><link rel="prefetch" href="/assets/js/43.06bf748b.js"><link rel="prefetch" href="/assets/js/44.ebd514b5.js"><link rel="prefetch" href="/assets/js/45.12908515.js"><link rel="prefetch" href="/assets/js/46.78fb0130.js"><link rel="prefetch" href="/assets/js/47.1a3b6cfd.js"><link rel="prefetch" href="/assets/js/48.ef04cdb5.js"><link rel="prefetch" href="/assets/js/49.763fd944.js"><link rel="prefetch" href="/assets/js/5.e50a6b6f.js"><link rel="prefetch" href="/assets/js/50.c8a7a24d.js"><link rel="prefetch" href="/assets/js/51.aaa9e6c7.js"><link rel="prefetch" href="/assets/js/52.7d3a85df.js"><link rel="prefetch" href="/assets/js/53.00784a7d.js"><link rel="prefetch" href="/assets/js/54.bd3845dd.js"><link rel="prefetch" href="/assets/js/55.10a67e4b.js"><link rel="prefetch" href="/assets/js/56.eb909b5a.js"><link rel="prefetch" href="/assets/js/57.4b821666.js"><link rel="prefetch" href="/assets/js/58.1db8af1f.js"><link rel="prefetch" href="/assets/js/59.d3045373.js"><link rel="prefetch" href="/assets/js/6.f25d4964.js"><link rel="prefetch" href="/assets/js/61.eca4de24.js"><link rel="prefetch" href="/assets/js/62.be7238a5.js"><link rel="prefetch" href="/assets/js/63.a6ace044.js"><link rel="prefetch" href="/assets/js/64.bbd88e61.js"><link rel="prefetch" href="/assets/js/65.1f6c1848.js"><link rel="prefetch" href="/assets/js/66.d22e96e8.js"><link rel="prefetch" href="/assets/js/67.80ad6427.js"><link rel="prefetch" href="/assets/js/68.130aa1f6.js"><link rel="prefetch" href="/assets/js/69.4b778299.js"><link rel="prefetch" href="/assets/js/7.9c54e368.js"><link rel="prefetch" href="/assets/js/70.edcac904.js"><link rel="prefetch" href="/assets/js/71.90aa842c.js"><link rel="prefetch" href="/assets/js/72.4cb8aeae.js"><link rel="prefetch" href="/assets/js/73.6f272f7b.js"><link rel="prefetch" href="/assets/js/74.d36a7a16.js"><link rel="prefetch" href="/assets/js/75.362400fa.js"><link rel="prefetch" href="/assets/js/76.140673a6.js"><link rel="prefetch" href="/assets/js/77.5f1336bf.js"><link rel="prefetch" href="/assets/js/78.6105e050.js"><link rel="prefetch" href="/assets/js/79.dd680c04.js"><link rel="prefetch" href="/assets/js/8.786d8198.js"><link rel="prefetch" href="/assets/js/80.ebfb944f.js"><link rel="prefetch" href="/assets/js/81.892d344c.js"><link rel="prefetch" href="/assets/js/9.b0b77105.js">
    <link rel="stylesheet" href="/assets/css/0.styles.331bc503.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container no-sidebar"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">Finen's Blog</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/post/" class="nav-link">
  文章
</a></div><div class="nav-item"><a href="/archives/" class="nav-link">
  归档
</a></div><div class="nav-item"><a href="/tags/" class="nav-link">
  标签云
</a></div><div class="nav-item"><a href="/friends/" class="nav-link">
  友人帐
</a></div> <a href="https://github.com/hirCodd" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/post/" class="nav-link">
  文章
</a></div><div class="nav-item"><a href="/archives/" class="nav-link">
  归档
</a></div><div class="nav-item"><a href="/tags/" class="nav-link">
  标签云
</a></div><div class="nav-item"><a href="/friends/" class="nav-link">
  友人帐
</a></div> <a href="https://github.com/hirCodd" target="_blank" rel="noopener noreferrer" class="repo-link">
    GitHub
    <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></nav>  <!----> </aside> <main class="page"> <div class="title-content"><div class="el-card box-cards is-never-shadow"><!----><div class="el-card__body"><h2>git 分支操作命令</h2> <div class="page-info"><i aria-hidden="true" class="el-icon-s-custom"></i> <span class="i-text">Finen</span> <i aria-hidden="true" class="el-icon-collection-tag"><span class="i-text">git</span></i> <i aria-hidden="true" class="el-icon-time"></i> <span class="i-text">2018-03-30</span> <span id="/blog/others/git/git-branch-command.html" data-flag-title="git 分支操作命令" class="leancloud-visitors"><i aria-hidden="true" class="el-icon-view"></i> <i class="leancloud-visitors-count">1000000</i></span></div></div></div></div> <div class="theme-default-content content__default"><h1 id="git分支操作"><a href="#git分支操作" class="header-anchor">#</a> git分支操作</h1> <h2 id="创建与合并分支"><a href="#创建与合并分支" class="header-anchor">#</a> 创建与合并分支</h2> <p>在版本回滚与更新中，git都是用master分支作为主线，通过时间顺序进行更新或者回滚。</p> <h3 id="创建分支"><a href="#创建分支" class="header-anchor">#</a> 创建分支</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>git checkout -b dev 
==（相当于）
git branch dev
git checkout dev
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>查看分支信息：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git branch
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_branch.png" alt="git_branch"></p> <p>现在已经在dev分支目录下，接下来可以在demo.txt中添加信息，我们添加一行数据：1111111111111</p> <p>此时，我们在dev目录下添加内容，并且提交到暂存区，然后commit。</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_branch3.png" alt="git_branch"></p> <p>接着，我们将目前分支切换到master主分支，查看文件新添加的内容是否存在。使用以下命令：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git checkout master
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_branch4.png" alt="git_branch4"></p> <p>然后，发现master分支上并没有我们想要看到的内容，所以我们需要合并两个分支。</p> <h3 id="合并分支"><a href="#合并分支" class="header-anchor">#</a> 合并分支</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>git merge 分支名
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>在master分之下合并分dev分支。使用以下命令：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git merger dev
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_merge.png" alt="git_merge"></p> <p>合并完成后即可删除没有用的额分支了，使用以下命令进行删除分支：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git branch -d dev
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_delete.png" alt="git_branch_delete"></p> <p>所有命令总结：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>查看分支：git branch
创建分支：git branch name
切换分支：git checkout name
创建+切换分支：git checkout -b name
合并某个分支到当前分支：git merge name
删除某个分支：git branch -d name
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="处理分支矛盾"><a href="#处理分支矛盾" class="header-anchor">#</a> 处理分支矛盾</h3> <p>新建一个分支，并且other中添加数据：2222222222222，并且检查文件。</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_branch_other.png" alt="git_branch_other"></p> <p>切换分支到master之中，并且master分支中增加内容：3333333333333，在增加之前使用cat命令，查看master坟之中是否存在2222222222222，增加后是否存在3333333333333。</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_checkout1.png" alt="git_checkout2"></p> <p>master分支数据如上，接下来我们继续合并分支。</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_merge2.png" alt="git_merge"></p> <p>由于我之前在master分支写了一个2222222的信息，，所以这有一个2222222222222222222/33333333333333333这样的信息。不过这样的错误信息也不影响分支是错误。<font color="red">Git用&lt;&lt;&lt;&lt;&lt;&lt;&lt;，=======，&gt;&gt;&gt;&gt;&gt;&gt;&gt;标记出不同分支的内容，其中&lt;&lt;&lt;HEAD是指主分支修改的内容，&gt;&gt;&gt;&gt;&gt;fenzhi1 是指fenzhi1上修改的内容，我们可以修改下如下后保存。修改后就是上面的最后一个图。</font></p> <p>然后我们就可以git log查看commit后的信息，在此我就截图了哈。</p> <h3 id="分支策略"><a href="#分支策略" class="header-anchor">#</a> 分支策略</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>1.创建一个dev分支。
2.修改demo.txt内容。
3.添加到暂存区。
4.切换回主分支(master)。
5.合并dev分支，使用命令 git merge –no-ff -m &quot;注释&quot; dev
6.查看历史记录
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>具体命令如下：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git checkout -b dev
在demo.txt文件中添加数据4444444444444
git add demo.txt
git commit -m &quot;add merge&quot;
git merge --no-ff -m &quot;merge with no-ff&quot; dev
git log 或者git log --graph --pretty=oneline --abbrev-commit
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_dev.png" alt="git_dev"></p> <p><strong>分支策略：首先master主分支应该是非常稳定的，也就是用来发布新版本，一般情况下不允许在上面干活，干活一般情况下在新建的dev分支上干活，干完后，比如上要发布，或者说dev分支代码稳定后可以合并到主分支master上来。</strong></p> <h2 id="bug分支"><a href="#bug分支" class="header-anchor">#</a> Bug分支</h2> <p>在开发中，会经常碰到bug问题，那么有了bug就需要修复，在Git中，分支是很强大的，每个bug都可以通过一个临时分支来修复，修复完成后，合并分支，然后将临时的分支删除掉。</p> <p>比如我在开发中接到一个404 bug时候，我们可以创建一个404分支来修复它，但是，当前的dev分支上的工作还没有提交。命令如下：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git checkout -b dev 
修改demo.txt文件添加数据：5555555555555
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_branch5.png" alt="git_branch5"></p> <p>并不是我不想提交，而是工作进行到一半时候，我们还无法提交，比如我这个分支bug要2天完成，但是我issue-404 bug需要5个小时内完成。怎么办呢？还好，Git还提供了一个stash功能，可以把当前工作现场 ”隐藏起来”，等以后恢复现场后继续工作。</p> <p>隐藏分支：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git stash
git status
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_stash.png" alt="git_stash"></p> <p>首先我们要确定在那个分支上修复bug，比如我现在是在主分支master上来修复的，现在我要在master分支上创建一个临时分支。</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_bug.png" alt="git_bug"></p> <p>修改后合并分支，然后删除bug分支即可。</p> <p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_bug2.png" alt="git_bug2"></p> <p>接着，我们继续回到dev分支下：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git checkout dev
git status
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>发现并没有文件，是因为stash隐藏了，所以我们需要从stash恢复。恢复命令如下：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git stash list
git stash apply
git stash drop
cat demo.txt
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>或者：</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>git stash list
git stash pop
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><img src="https://finen-1251602255.cos.ap-shanghai.myqcloud.com/images/blog/git_stash3.png" alt="git_stash"></p></div> <footer class="page-edit"><!----> <!----></footer> <!---->  <div class="vcomment"><div id="vcomments"></div></div></main></div><div class="global-ui"><!----><!----></div></div>
    <script src="/assets/js/app.8657dc36.js" defer></script><script src="/assets/js/3.655fda8f.js" defer></script><script src="/assets/js/60.e1ccc619.js" defer></script><script src="/assets/js/11.32858d9c.js" defer></script>
  </body>
</html>
